package edu.neu.swc.skynet.document;

import edu.neu.swc.skynet.book.BookInfo;
import edu.neu.swc.skynet.book.BookShow;
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;
import org.springframework.data.mongodb.core.mapping.Document;
import org.springframework.data.mongodb.core.mapping.Field;

/**
 * <h1>图书文档类</h1>
 * 图书的实体类
 * 对应 MongoDB 的一个 Collection
 * 文档定义：
 * {
 *     "id": 11,
 *     "book_name": "深入理解Java虚拟机：JVM高级特性与实践（第3版）",
 *     "book_author": "周志明",
 *     "book_press": "机械工业出版社",
 *     "book_release": "2019-12",
 *     "book_price": 129.00,
 *     "book_type": "计算机/网络",
 *     "book_image": "http://img3m5.ddimg.cn/55/8/28495225-1_b_9.jpg"
 * }
 */
@Data
@NoArgsConstructor
@AllArgsConstructor
@Document(collection = "books")
public class SkyNetBookDoc {

    @Field(value = "id")
    private Long id;

    @Field(value = "book_name")
    private String bookName;

    @Field(value = "book_author")
    private String bookAuthor;

    @Field(value = "book_press")
    private String bookPress;

    @Field(value = "book_release")
    private String bookRelease;

    @Field(value = "book_price")
    private Double bookPrice;

    @Field(value = "book_type")
    private String bookType;

    @Field(value = "book_image")
    private String bookImage;

    /**
     * <h2>BookInfo -> SkyNetBookDoc</h2>
     * @param bookInfo 传递的 BookInfo 对象
     * @return 转换完成的 SkyNetBookDoc 对象
     */
    public static SkyNetBookDoc to(BookInfo bookInfo){

        SkyNetBookDoc skyNetBookDoc = new SkyNetBookDoc();

        skyNetBookDoc.setId(bookInfo.getId());
        skyNetBookDoc.setBookName(bookInfo.getBookName());
        skyNetBookDoc.setBookAuthor(bookInfo.getBookAuthor());
        skyNetBookDoc.setBookPress(bookInfo.getBookPress());
        skyNetBookDoc.setBookRelease(bookInfo.getBookRelease());
        skyNetBookDoc.setBookType(bookInfo.getBookType());
        skyNetBookDoc.setBookPrice(bookInfo.getBookPrice());
        skyNetBookDoc.setBookImage(bookInfo.getBookImage());

        return skyNetBookDoc;
    }

    /**
     * <h2>SkyNetBookDoc -> BookInfo</h2>
     * @return 转换完成的 BookInfo
     */
    public BookInfo toBookInfo(){

        BookInfo bookInfo = new BookInfo();

        bookInfo.setId(this.getId());
        bookInfo.setBookName(this.getBookName());
        bookInfo.setBookImage(this.getBookImage());
        bookInfo.setBookAuthor(this.getBookAuthor());
        bookInfo.setBookPress(this.getBookPress());
        bookInfo.setBookPrice(this.getBookPrice());
        bookInfo.setBookType(this.getBookType());
        bookInfo.setBookRelease(this.getBookRelease());

        return bookInfo;
    }

    /**
     * <h2>SkyNetBook -> BookShow</h2>
     * @return 转换完成的 BookShow 对象
     */
    public BookShow toBookShow(){

        BookShow bookShow = new BookShow();

        bookShow.setBookName(this.getBookName());
        bookShow.setBookImage(this.getBookImage());
        bookShow.setBookAuthor(this.getBookAuthor());
        bookShow.setBookPress(this.getBookPress());
        bookShow.setBookPrice(this.getBookPrice());
        bookShow.setBookType(this.getBookType());

        return bookShow;
    }
}
